package com.focus.wecenter.web.controller;

import com.focus.wecenter.pojo.User;
import com.focus.wecenter.service.UserService;
import com.focus.wecenter.util.Message;
import com.focus.wecenter.web.controller.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 用户中心信息
 *
 * @author Lv Jing
 * @date 2018年01月09日 17:28
 */
@Controller
@RequestMapping("/people")
public class UserInfoController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping("/{username}")
    public String showUserInfo(@PathVariable String username, Model model) {
        User user = userService.findByUsername(username);
        model.addAttribute("info", user);
        return "user/user_info";
    }

    /**
     * 关注或取关用户
     */
    @RequestMapping(value = "/ajax/follow_people/", method = RequestMethod.POST)
    @ResponseBody
    public Message followPeople(int userID, HttpSession session) {
        try {
            return userService.focusUser(userID, this.getSessionUserID(session));
        } catch (Exception e) {
            e.printStackTrace();
            Message message = new Message();
            message.setData("服务器未知异常");
            message.setSuccess(false);
            return message;
        }
    }
}
